package com.moneyhelper.common.biz;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moneyhelper.common.mapper.CommonMapper;
import com.moneyhelper.common.resp.TableResponse;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2019/06/30
 * @Description：
 */
public abstract class CommonBiz<M extends CommonMapper<T>,T> {

    @Autowired
    protected M mapper;

    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    public List<T> selectAll() {
        return mapper.selectAll();
    }

    public Long selectCount(T entity) {
        return new Long(mapper.selectCount(entity));
    }

    public int insertSelective(T entity) {
       return mapper.insertSelective(entity);
    }

    public int insertList(List<T> list) {
        return mapper.insertList(list);
    }

    public int delete(T entity) {
        return mapper.delete(entity);
    }

    public int deleteById(Object id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int deleteByIds(String id) {
        return mapper.deleteByIds(id);
    }

    public int deleteByIds(List<String> idList) {
        if (CollUtil.isNotEmpty(idList)) {
            String ids = CollUtil.join(idList, ",");
            return mapper.deleteByIds(ids);
        }
        return 0;
    }

    public int updateById(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    public int updateSelectiveById(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    public TableResponse<T> selectByQuery(Map<String, Object> queryParams) {
        //当前页码
        int page = 1;
        //每页条数
        int limit = 10;
        //分页参数
        if(queryParams.get("page")!=null) {
            page = Integer.parseInt(queryParams.get("page").toString());
            queryParams.remove("page");
        }
        if(queryParams.get("limit")!=null) {
            limit = Integer.parseInt(queryParams.get("limit").toString());
            queryParams.remove("limit");
        }
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        Example example = new Example(clazz);
        query2criteria(queryParams, example);
        Page<Object> result = PageHelper.startPage(page, limit);
        List<T> list = this.selectByExample(example);
        PageInfo<T> pageInfo = new PageInfo<>(list);
        return new TableResponse<T>(pageInfo.getTotal(), list);
    }

    public void query2criteria(Map<String, Object> query, Example example) {
        if (query.entrySet().size() > 0) {
            Example.Criteria criteria = example.createCriteria();
            for (Map.Entry<String, Object> entry : query.entrySet()) {
                criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
            }
        }
    }
}
